package com.yunji.framework_template.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.yunji.framework_template.biz.cache.CacheService;
import com.yunji.framework_template.biz.service.NewsService;
import com.yunji.framework_template.common.util.NumberUtil;
import com.yunji.framework_template.common.web.Page;
import com.yunji.framework_template.common.web.Result;
import com.yunji.framework_template.orm.persistence.model.News;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by fenglibin on 2018/11/26.
 */
@Slf4j
@Controller
@RequestMapping("/news/")
public class NewsController {
    @Value("#{settings['system.domain']}")
    private String      systemDomain;
    @Value("#{settings['http.protocol']}")
    private String      httpProtocol;

    @Resource
    private NewsService                  newsService;
    @Resource
    private CacheService cacheService;

    @RequestMapping("selectAll1")
    @ResponseBody
    public Result<List<News>> selectAll() {
        return Result.success(newsService.selectAll());
    }

    @RequestMapping("select")
    @ResponseBody
    public Result<News> selectByPrimaryKey(Long id) {
        return Result.success(newsService.selectByPrimaryKey(id));
    }

    @RequestMapping("selectByCondition")
    @ResponseBody
    public Result<List<News>> selectByCondition(News record) {
        return Result.success(newsService.selectByCondition(record));
    }

    @RequestMapping("count")
    @ResponseBody
    public Result<Long> count(News record) {
        return Result.success(newsService.count(record));
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result<Integer> insertSelective(News record) {
        return Result.success(newsService.insertSelective(record));
    }

    @RequestMapping("update")
    @ResponseBody
    public Result<Integer> updateByPrimaryKeySelective(News record) {
        return Result.success(newsService.updateByPrimaryKeySelective(record));
    }

    @RequestMapping("selectByPage")
    @ResponseBody
    public Result<Page<List<News>>> selectByPage(Page<News> page) {
        List<News> list = null;
        page = page.sequentialDisplayCalculate(newsService.count(page.getObj()));
        if (page.getTotalCount() > 0) {
            list = newsService.selectByPage(page);
        }
        Page<List<News>> result = new Page<List<News>>(page, list);
        return Result.success(result);
    }

    @RequestMapping("deleteByCondition1")
    @ResponseBody
    public Result<Integer> deleteByCondition(News record) {
        return Result.success(newsService.deleteByCondition(record));
    }

    @RequestMapping("delete1")
    @ResponseBody
    public Result<Integer> deleteByPrimaryKey(Long id) {
        return Result.success(newsService.deleteByPrimaryKey(id));
    }

    /**
     * Home page
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws HttpException,
                                                                                        IOException {
        log.info("News index is accessed.");
        ModelAndView view = new ModelAndView("news/news_index");
        view.getModelMap().addAttribute("indexType", "News");
        view.getModelMap().addAttribute("systemDomain", systemDomain);
        String currentPage = request.getParameter("currentPage");
        int currentPageInt = 1;
        if (NumberUtil.isNumber(currentPage)) {
            currentPageInt = Integer.parseInt(currentPage);
        }
        List<News> newsList = newsService.getPageNews(currentPageInt);
        if (newsList != null) {
            view.getModelMap().put("newsList", newsList);
        }

        return view;
    }
    /**
     * Detail page
     */
    @RequestMapping(value = "/detail/{newsId}", method = RequestMethod.GET)
    public ModelAndView detail(@PathVariable("newsId") String newsId) throws HttpException,
                                                                                        IOException {
        log.info("Detail access newsId is:" + newsId);
        if (!NumberUtil.isNumber(newsId)) {
            log.warn("The newsId is not number:" + newsId);
            return new ModelAndView("404");
        }
        long nid = Long.parseLong(newsId);
        News news = newsService.selectByPrimaryKey(nid);
        if (news==null) {
            log.warn("Cannot get news by newsId:" + newsId);
            return new ModelAndView("404");
        }
        ModelAndView view = new ModelAndView("news/news_detail");
        view.getModelMap().addAttribute("systemDomain", systemDomain);
        view.getModelMap().addAttribute("httpProtocol", httpProtocol);
        
        view.getModelMap().put("news", news);

        return view;
    }
    @RequestMapping(value = "/go/{newsId}", method = RequestMethod.GET)
    public ModelAndView go(@PathVariable("newsId") String newsId) throws HttpException, IOException {
        log.info("Access newsId is:" + newsId);
        if (!NumberUtil.isNumber(newsId)) {
            log.warn("The newsId is not number:" + newsId);
            return new ModelAndView("404");
        }
        long nid = Long.parseLong(newsId);
        News news = newsService.selectByPrimaryKey(nid);
        if (news==null) {
            log.warn("Cannot get news by newsId:" + newsId);
            return new ModelAndView("404");
        }
        newsService.addViewsByPrimaryKey(news);
        return new ModelAndView(new RedirectView(news.getUrl()));
    }

}
